package cn.stard.user.service.impl;

import cn.stard.basic.constants.BaseConstants;
import cn.stard.basic.dto.SmsCodeDot;
import cn.stard.basic.exception.BusinessException;
import cn.stard.basic.util.MD5Utils;
import cn.stard.basic.util.StrUtils;
import cn.stard.user.domain.Logininfo;
import cn.stard.user.domain.User;
import cn.stard.user.mapper.LogininfoMapper;
import cn.stard.user.mapper.UserMapper;
import cn.stard.user.service.IUserService;
import cn.stard.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stard
 * @since 2022-07-06
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LogininfoMapper logininfoMapper;
    @Override
    public void phoneCodeResister(SmsCodeDot smsCodeDot) {

        /*先把前端传入对象的参数一一取出来再做出相应的判断让程序完整没有漏洞*/
        String phone = smsCodeDot.getPhone();
        /*获取第一次输入的密码*/
        String password = smsCodeDot.getPassword();
        /*获取第二次输入的密码*/
        String confirmPassword = smsCodeDot.getConfirmPassword();
        /*获取输入的验证码是否正确*/
        String smsCodeValue = smsCodeDot.getSmsCodeValue();
         /*设置拼接手机验证码*/
        String phoneCodekey=  BaseConstants.VerifyConstants.BUSINESS_REGISTER_KEY+ phone;
        /*获取存在与redis中的值*/
        Object phoneCodeValue= redisTemplate.opsForValue().get(phoneCodekey);
        // 判断输入的参数是否为空
        if (StringUtils.isEmpty(password)||
                StringUtils.isEmpty(confirmPassword)||
                StringUtils.isEmpty(smsCodeValue)  ){
                throw new BusinessException("参数不能为空,请重新输入");
        }
            /*判断两次的密码是否一致*/
        if (!password.equals(confirmPassword)){
            throw new BusinessException("两次密码不一致");
        }
        /*判断存在与redis的值是否失效*/
         if (phoneCodeValue==null){
             throw new BusinessException("验证码已失效请重新输入");
         }
         /*取出字符串拼接的验证码值*/
        String phoneCode= phoneCodeValue.toString().split(":")[1];
        /*判断创建的验证码大小写是否相等*/
            if (!phoneCode.equalsIgnoreCase(smsCodeValue)){
                throw new BusinessException("验证码不一致,请重新输入");
            }
            /*传入电话号码来判断是否注册*/
        User user = userMapper.loadByPhone(phone);
        if (user!=null){
            throw  new BusinessException("对象已注册,请直接登录如果忘记密码请输入密码");
        }
        /*注册成功之后需要把他们的参数放入数据库中*/
        user =smsCodeDto2User(smsCodeDot);
        // 保存user表
        userMapper.save(user);
        Logininfo logininfo = user2Logininfo(user);
        /*保存到logininfo表*/
        logininfoMapper.save(logininfo);
        /*将logininfo主键设置到user表并且修改*/
        user.setLogininfoId(logininfo.getId());
        // 添加之后修改查看效果
        userMapper.update(user);
    }

    @Override
    public void emailCodeResister(SmsCodeDot smsCodeDot) {
        /*获取第一次输入的密码*/
        String password = smsCodeDot.getPassword();
        /*获取第二次输入的密码*/
        String confirmPassword = smsCodeDot.getConfirmPassword();
        /*邮箱*/
        String email = smsCodeDot.getEmail();

        // 判断输入的参数是否为空
        if (StringUtils.isEmpty(password)||
                StringUtils.isEmpty(confirmPassword)||
                StringUtils.isEmpty(email)  ){
            throw new BusinessException("参数不能为空,请重新输入");
        }

        /*判断两次的密码是否一致*/
        if (!password.equals(confirmPassword)){
            throw new BusinessException("两次密码不一致");
        }

        User user =  userMapper.loadByemail(email);
        if (user!=null){
            throw  new BusinessException("邮箱已注册,请直接登录如果忘记密码请输入密码");
        }

        /*注册成功之后需要把他们的参数放入数据库中*/
        user =smsCodeDto2User(smsCodeDot);
        // 保存user表
        userMapper.save(user);
        Logininfo logininfo = user2Logininfo(user);
        /*保存到logininfo表*/
        logininfoMapper.save(logininfo);
        /*将logininfo主键设置到user表并且修改*/
        user.setLogininfoId(logininfo.getId());
        // 添加之后修改查看效果
        userMapper.update(user);

    }

    @Override
    public User loadBylogininfo(Long id) {

        return userMapper.loadBylogininfo(id);
    }

    /*封装一个加盐值加密的一个密码到user里面 */
    private User smsCodeDto2User(SmsCodeDot smscodeDto){
        System.out.println("打印数据查看smscodeDto值"+smscodeDto);
        String phone = smscodeDto.getPhone();
        String email = smscodeDto.getEmail();
        User user = new User();
        user.setUsername(phone);
        user.setPhone(phone);
        user.setEmail(email);
        /*设置随机字符位数*/
        String salt = StrUtils.getComplexRandomString(32);
        String saltPassword = MD5Utils.encrypByMd5(salt + smscodeDto.getPassword());
        user.setPassword(saltPassword);
        user.setSalt(salt);
        return user;
    }
 /*把表封装到登录页面里面 通过已经创建的user来作用 创建的构造方法*/
    private Logininfo user2Logininfo(User user){
        Logininfo logininfo = new Logininfo();
        /*把user的所有给logininfo*/
        BeanUtils.copyProperties(user,logininfo);
        /*设置登录的是用户*/
        logininfo.setType(1);
        return logininfo;
    }
}
